#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author Jmz
import os
from gevent import monkey,spawn;monkey.patch_all()
from multiprocessing import Process
from socket import *

def work(service):
    while True:
        conn,addr = service.accept()
        spawn(web,conn,addr)

def main(ip_addr):
    service = socket(AF_INET,SOCK_STREAM)
    service.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
    service.bind(ip_addr)
    service.listen(5)

    work(service)

def web(conn,addr):
    try:
        buff = conn.recv(1024)
        if not buff:raise TypeError("aaa")
        conn.send("HTTP/1.1 200 OK\r\n\r\n".encode("utf-8"))
        with open("index.html","rb") as f:
            for line in f:
                conn.send(line)
        conn.close()
    except Exception as e:
        print(e)
    finally:
        conn.close()


if __name__ == '__main__':
    main(("127.0.0.1",8001))